package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.IPcPrdInfoRuleService;
import com.wondertek.poms.dao.po.PrdInfoRule;
import com.wondertek.poms.dao.po.PrdRuleSecondClassify;
import com.wondertek.poms.dao.repo.IPcPrdInfoRuleDao;
import com.wondertek.poms.dao.repo.IPrdRuleSecondClassifyDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 商品包规则 service层实现 </p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Service
public class PcPrdInfoRuleServiceImpl extends ServiceImpl<PrdInfoRule, Long> implements IPcPrdInfoRuleService {

	@Autowired
	private IPcPrdInfoRuleDao prdInfoRuleDao;
	@Autowired
	private IPrdRuleSecondClassifyDao prdRuleSecondClassifyDao;

	@Override
	public PrdInfoRule deleteLogic(Long id) {
		return null;
	}

	@Override
	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#model.prdInfoId", beforeInvocation = true)
	public PrdInfoRule save(PrdInfoRule model) {
		return super.save(model);
	}

	@Override
	@Transactional
	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#cp.prdInfoId", beforeInvocation = true)
	public PrdInfoRule delete(PrdInfoRule cp) {
		prdRuleSecondClassifyDao.deleteAllByPrdRuleId(cp.getPrdRuleId());
		jpaRepository.deleteById(cp.getPrdRuleId());
		return cp;
	}

	@Override
	public List<PrdInfoRule> findAllByPrdInfoIdAndIsprdSel(Long prdInfoId, Integer isprdSel) {
		return prdInfoRuleDao.findAllByPrdInfoIdAndIsprdSel(prdInfoId, isprdSel);
	}

	@Transactional
	@Override
	public int deleteByPrdRuleId(List<Long> ids) {
		for (Long id : ids) {
			prdRuleSecondClassifyDao.deleteAllByPrdRuleId(id);
			prdInfoRuleDao.deleteByPrdRuleId(id);
		}
		return ids.size();
	}

	@Override
	@Transactional
	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#model.prdInfoId", beforeInvocation = true)
	public PrdInfoRule merge(PrdInfoRule model) {
		PrdInfoRule.putTimeAndUser(model);
		PrdInfoRule dbo = jpaRepository.getOne(model.getPrdRuleId());
		CopyUtils.copyProperties(model, dbo);
		return jpaRepository.save(dbo);
	}

	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#model.prdInfoId", beforeInvocation = true)
	public PrdInfoRule update(PrdInfoRule model) {
		PrdInfoRule.putTimeAndUser(model);
		PrdInfoRule dbo = jpaRepository.getOne(model.getPrdRuleId());
		BeanUtils.copyProperties(model, dbo);
		return jpaRepository.save(dbo);
	}

	@Transactional
	@Override
	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#infoRule.prdInfoId", beforeInvocation = true)
	public PrdInfoRule saveRuleAndSecondClassify(PrdInfoRule infoRule) {
		PrdInfoRule save = prdInfoRuleDao.save(infoRule);
		Set<PrdRuleSecondClassify> secondClassifys = infoRule.getSecondClassifys();
		if (secondClassifys != null && secondClassifys.size() > 0) {
			for (PrdRuleSecondClassify secondClassify : secondClassifys) {
				secondClassify.setPrdRuleId(save.getPrdRuleId());
				prdRuleSecondClassifyDao.save(secondClassify);
			}
		}
		return save;
	}

	@Override
	@Cacheable(value = "poms-service:pc-prdInfo-rule", key = "#prdInfoId", unless = "#result == null")
	public List<PrdInfoRule> findAllByPrdInfoId(Long prdInfoId) {
		PrdInfoRule params = new PrdInfoRule();
		params.setPrdInfoId(prdInfoId);
		List<PrdInfoRule> pirs = super.findAll(params);
		for (PrdInfoRule pir : pirs) {
			Set<PrdRuleSecondClassify> set = prdRuleSecondClassifyDao.findAllByPrdRuleId(pir.getPrdRuleId());
			pir.setSecondClassifys(set);
		}
		return pirs;
	}

	@CacheEvict(value = "poms-service:pc-prdInfo-rule", key = "#model.prdInfoId", beforeInvocation = true)
	@Transactional
	@Override
	public PrdInfoRule updateRuleAndSecondClassify(PrdInfoRule model) {
		PrdInfoRule dbo = jpaRepository.getOne(model.getPrdRuleId());
		BeanUtils.copyProperties(model, dbo);
		PrdInfoRule rule = jpaRepository.save(dbo);
		Set<PrdRuleSecondClassify> classifys = model.getSecondClassifys();
		HashSet<PrdRuleSecondClassify> set = new HashSet<>();
		if (classifys != null && classifys.size() > 0) {
			for (PrdRuleSecondClassify classify : classifys) {
				PrdRuleSecondClassify one = prdRuleSecondClassifyDao.getOne(classify.getId());
				BeanUtils.copyProperties(classify, one);
				PrdRuleSecondClassify save = prdRuleSecondClassifyDao.save(one);
				set.add(save);
			}
		}
		rule.setSecondClassifys(set);
		return jpaRepository.save(dbo);

	}

	@Override
	public PrdInfoRule findByPrdRuleId(Long prdRuleId) {
		return prdInfoRuleDao.findByPrdRuleId(prdRuleId);
	}
}